package com.zhuoyue.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletRequest;


import com.zhuoyue.common.core.controller.ControllerResult;
import com.zhuoyue.system.domain.dto.CommonReq;
import com.zhuoyue.system.domain.dto.ControlReq;
import com.zhuoyue.system.domain.dto.LocalScenePoReqDTO;
import com.zhuoyue.system.domain.po.TblLocalScenePo;
import com.zhuoyue.system.domain.vo.LocalSceneVo;
import com.zhuoyue.system.mapper.SysDeptMapper;
import com.zhuoyue.system.mapper.TblLocalSceneMapper;
import com.zhuoyue.web.utils.OrgCodeUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.zhuoyue.system.service.ITblLocalSceneService;

import com.zhuoyue.common.annotation.Log;
import com.zhuoyue.common.core.controller.BaseController;
import com.zhuoyue.common.core.domain.AjaxResult;
import com.zhuoyue.common.core.page.TableDataInfo;
import com.zhuoyue.common.enums.BusinessType;
import com.zhuoyue.common.utils.poi.ExcelUtil;
import org.springframework.web.client.RestTemplate;

/**
 * 本地场景Controller
 *
 * @author hewenze
 * @date 2020-08-10
 */
@RestController
@Api(value = "场景接口", tags = "场景接口")
@RequestMapping("/system/scene")
public class TblLocalSceneController extends BaseController {
    @Autowired
    private ITblLocalSceneService tblLocalSceneService;

    @Autowired
    private TblLocalSceneMapper sceneMapper;

    @Autowired
    private OrgCodeUtils orgCodeUtils;
    @Autowired
    private SysDeptMapper sysDeptMapper;

    @Value("${zyzn.scene.control}")
    private String sceneUrl;

    /**
     * 查询本地场景列表
     */
    @GetMapping("/list")
    @ApiOperation(value = "查询场景列表", notes = "查询场景列表")
    public TableDataInfo list(LocalScenePoReqDTO localScenePoReq) {
//        if (localScenePoReq.getDeptId() == null) {
//            localScenePoReq.setOrganizationCode(orgCodeUtils.getOrgCode(request));
//        } else {
//            SysDept sysDept = sysDeptMapper.selectDeptById(localScenePoReq.getDeptId());
//            localScenePoReq.setOrganizationCode(sysDept.getOrgCode());
//        }
        TblLocalScenePo tblLocalScenePo = new TblLocalScenePo().dtoToDo(localScenePoReq);

        startPage();
        List<LocalSceneVo> list = tblLocalSceneService.selectTblLocalSceneList(tblLocalScenePo);
        return getDataTable(list);
    }


    /**
     * 查询本地场景列表
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "查询场景信息", notes = "查询场景信息")
    public AjaxResult list(@PathVariable("id") String id) {
        LocalSceneVo localSceneVo = tblLocalSceneService.selectById(id);
        return AjaxResult.success(localSceneVo);
    }

    /**
     * 导出本地场景列表
     */
    @ApiOperation(value = "导出场景列表", notes = "导出场景列表")
    @Log(title = "本地场景", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TblLocalScenePo tblLocalScenePo, HttpServletRequest request) {
        tblLocalScenePo.setOrganizationCode(orgCodeUtils.getOrgCode(request));
        tblLocalScenePo.setPageNum(null);
        tblLocalScenePo.setPageSize(null);
        List<LocalSceneVo> list = tblLocalSceneService.selectTblLocalSceneList(tblLocalScenePo);
        ExcelUtil<LocalSceneVo> util = new ExcelUtil<LocalSceneVo>(LocalSceneVo.class);
        return util.exportExcel(list, "场景");
    }

    @GetMapping("/listByHomeId")
    public TableDataInfo listSceneByHomeId(TblLocalScenePo tblLocalScenePo) {
        startPage();
        List<LocalSceneVo> localSceneVos = sceneMapper.selectSceneListByHomeId(tblLocalScenePo);
        return getDataTable(localSceneVos);
    }



    @PostMapping("/control")
    public AjaxResult triggerScene(String sceneId) {
        CommonReq commonReq = new CommonReq();
        ControlReq controlReq = new ControlReq();
        controlReq.setControlType("SCENE_TRIGGER");

        commonReq.setDeviceId(sceneId);
        commonReq.setPurpose("CONTROL");
        commonReq.setControlParam(controlReq);

        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<ControllerResult> colRsuResp = restTemplate.postForEntity(sceneUrl, commonReq, ControllerResult.class);

        if (colRsuResp.getStatusCode() == HttpStatus.OK) {
            return AjaxResult.success();
        } else {
            return AjaxResult.error();
        }
    }
}
